const axios = require('axios');
const MockAdapter = require('axios-mock-adapter');
const normalAxios = axios.create();
const mockAxios = axios.create();
import _config from "./config";
import reqTools from "../utils/reqTools";

// mock 数据
var mock = new MockAdapter(mockAxios);

mock.onGet('/getAllUser').reply((config) => {
  return new Promise(function(resolve, reject) {
    reqTools.get('/api/sysUser',config.params).then(res => {
      resolve([200, res.data]);
    }).catch((err) => {
      resolve([500, err ]);
    });
  });
});
mock.onGet('/getAllUserById').reply((config) => {
  return new Promise(function(resolve, reject) {
    const id= config.id;
    console.log(id);
    reqTools.get('/api/sysUser/{id}?id='+id).then(res => {
      resolve([200, res.data]);
    }).catch((err) => {
      resolve([500, err ]);
    });
  });
});

mock.onGet('/queryUserRolesByUserId').reply((config) => {
  return new Promise(function(resolve, reject) {
    reqTools.get('/api/sysUser/queryUserRoles',config).then(res => {
      resolve([200, res.data]);
    }).catch((err) => {
      resolve([500, err ]);
    });
  });
});

mock.onDelete('/delUser').reply((config) => {
  return new Promise(function(resolve, reject) {
    const ids= config.ids;
    reqTools.delete('/api/sysUser/deletes',{ids}).then((res) => {
      resolve([200, res]);
    }).catch((err) => {
      resolve([500, err ]);
    });
  });
});
mock.onPut('/updateUser').reply((config) => {
  return new Promise(function(resolve, reject) {
    reqTools.put( '/api/sysUser', config.data, 
   ).then((res) => {
      resolve([200, res ]);
    }).catch((err) => {
      resolve([500, err ]);
    });
  });
});
mock.onPut('/resetPassword').reply((config) => {
  return new Promise(function(resolve, reject) {
   //var obj=eval(...config.data);
   var obj = eval('(' + config.data + ')'); 
    reqTools.put( '/api/sysUser/resetPassword?id='+obj.id
   ).then((res) => {
      resolve([200, res ]);
    }).catch((err) => {
      resolve([500, err ]);
    });
  });
});
mock.onPost('/addUser').reply((config) => {
  return new Promise(function(resolve, reject) {
    reqTools.post( '/api/sysUser',config.data
    ).then((res) => {
      resolve([200, res]);
    }).catch((err) => {
      resolve([500, err ]);
    });
  });
});
mock.onGet('/sysUser/getUserTargetCount').reply((config) => {
  return new Promise(function(resolve, reject) {
    reqTools.get('/api/sysUser/getUserTargetCount?value='+config.value).then(res => {
      resolve([200, res.data]);
    }).catch((err) => {
      resolve([500, err ]);
    });
  });
});
export default mockAxios;
